home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / chesssrc.zip / BOARD.C < prev    next >
Text File  |  1990-09-01  |  6KB  |  245 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Based on Ideas and code segments of Charles Petzold from artices in
  9.   MicroSoft Systems Journal.
  10.  
  11.  
  12.   This file is part of CHESS.
  13.  
  14.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  15.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  16.   the consequences of using it or for whether it serves any particular
  17.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  18.   General Public License for full details.
  19.  
  20.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  21.   only under the conditions described in the CHESS General Public License.
  22.   A copy of this license is supposed to have been given to you along with
  23.   CHESS so you can know your rights and responsibilities.  It should be in a
  24.   file named COPYING.  Among other things, the copyright notice and this
  25.   notice must be preserved on all copies.
  26. */
  27.  
  28. #define NOATOM 
  29. #define NOCLIPBOARD
  30. #define NOCREATESTRUCT
  31. #define NOSOUND
  32. #define NOWH
  33. #define NOWINOFFSETS
  34. #define NOCOMM
  35. #define NOKANJI
  36.  
  37. #include <windows.h>
  38. #include <stdio.h>
  39. #include "defs.h"
  40.  
  41. /* All units defined in pixels */
  42.  
  43. #define BRD_HORZFRONT   48
  44. #define BRD_HORZBACK    32
  45. #define BRD_VERT        32
  46. #define BRD_EDGE        8
  47. #define BRD_HORZMARGIN  32
  48. #define BRD_BACKMARGIN  5
  49. #define BRD_FRONTMARGIN 5
  50.  
  51. static void DrawOneSquare ( HDC hDC, short x, short y);
  52. static int HilitSq;
  53.  
  54. void QueryBoardSize ( POINT *pptl )
  55. {
  56.    pptl->x = 2*BRD_HORZMARGIN + 8*BRD_HORZFRONT;
  57.    pptl->y = BRD_BACKMARGIN + 8*BRD_VERT + 2*BRD_FRONTMARGIN + 2*BRD_EDGE;
  58. }
  59.  
  60. void QuerySqSize ( POINT *pptl ) {
  61.    pptl->x = BRD_HORZFRONT;
  62.    pptl->y = BRD_VERT;
  63. }
  64.  
  65. void QuerySqOrigin ( short x, short y, POINT *pptl)
  66. {
  67.    pptl->x = BRD_HORZMARGIN + y * (BRD_HORZFRONT-BRD_HORZBACK)/2 +
  68.              x * (y*BRD_HORZBACK + (8-y)*BRD_HORZFRONT)/8;
  69.    pptl->y = (BRD_BACKMARGIN+8*BRD_VERT+BRD_FRONTMARGIN)  - y*BRD_VERT;
  70. }
  71.  
  72. void QuerySqCoords ( short x, short y, POINT aptl[] )
  73. {
  74.    QuerySqOrigin ( x,  y,  aptl+0);
  75.    QuerySqOrigin ( x+1,y,  aptl+1);
  76.    QuerySqOrigin ( x+1,y+1,aptl+2);
  77.    QuerySqOrigin ( x,  y+1,aptl+3);
  78. }
  79.  
  80. static void DrawOneSquare ( HDC hDC, short x, short y)
  81. {
  82.    POINT aptl[4];
  83.  
  84.    QuerySqCoords ( x,y, aptl);
  85.    Polygon( hDC, aptl, 4);
  86. }
  87.  
  88. /*
  89.    Draw the board.  Pass the routine the upper left connor and the
  90.    colors to draw the squares.
  91. */
  92.  
  93. void Draw_Board ( HDC hDC, int reverse,
  94.                   DWORD DarkColor, DWORD LightColor )
  95. {
  96.    int x, y, OldPolyMode;
  97.    HBRUSH hOldBrush, hBrush_lt, hBrush_dk;
  98.    HPEN hOldPen;
  99.    POINT aptl[4];
  100.  
  101.    hBrush_lt = CreateSolidBrush ( LightColor );
  102.    hBrush_dk = CreateSolidBrush ( DarkColor );
  103.  
  104.    hOldBrush = SelectObject ( hDC, hBrush_lt);
  105.    hOldPen   = SelectObject ( hDC, GetStockObject (BLACK_PEN) );
  106.  
  107.  
  108.    OldPolyMode = SetPolyFillMode ( hDC, WINDING );
  109.  
  110.    for (y=0; y<8; y++) {
  111.       for (x=0; x<8; x++) {
  112.          if ( reverse == 0 ) {
  113.             SelectObject ( hDC, ((x+y)&1) ? hBrush_lt : hBrush_dk);
  114.             DrawOneSquare (hDC, x, y);
  115.          } else {
  116.             SelectObject ( hDC, (((7-x)+(7-y))&1) ? hBrush_lt : hBrush_dk);
  117.             DrawOneSquare (hDC, 7-x, 7-y);
  118.          }
  119.       }
  120.    }
  121.  
  122. /* Now draw the bottom edge of the board */
  123.  
  124.    for (x=0; x<8; x++) {
  125.       QuerySqCoords ( x,0, aptl);
  126.  
  127.       aptl[2].x = aptl[1].x;
  128.       aptl[2].y = aptl[1].y + BRD_EDGE;
  129.  
  130.       aptl[3].x = aptl[0].x;
  131.       aptl[3].y = aptl[0].y + BRD_EDGE;
  132.  
  133.       SelectObject ( hDC, (x&1) ? hBrush_lt : hBrush_dk);
  134.       Polygon ( hDC, aptl, 4 );
  135.    }
  136.    SetPolyFillMode (hDC, OldPolyMode);
  137.  
  138.    SelectObject (hDC, hOldPen);
  139.    SelectObject (hDC, hOldBrush);
  140.  
  141.    DeleteObject ( hBrush_lt);
  142.    DeleteObject ( hBrush_dk);
  143. }
  144.  
  145. void DrawCoords ( HDC hDC, int reverse, DWORD clrBackGround, DWORD clrText)
  146. {
  147.    HFONT hFont, hOldFont;
  148.    int i, OldBkMode;
  149.    DWORD OldBkColor, OldTextColor;
  150.    short xchar, ychar;
  151.    POINT pt;
  152.    TEXTMETRIC tm;
  153.  
  154.    hFont = CreateFont ( 13, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  155.                         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  156.                         DEFAULT_QUALITY, FIXED_PITCH | FF_SWISS, "Helv" );
  157.  
  158.    hOldFont = SelectObject ( hDC, hFont);
  159.    OldBkColor = SetBkColor ( hDC, clrBackGround);
  160.    OldBkMode = SetBkMode ( hDC, TRANSPARENT);
  161.    OldTextColor = SetTextColor (hDC, clrText);
  162.  
  163.    GetTextMetrics ( hDC, &tm);
  164.    xchar = tm.tmMaxCharWidth;
  165.    ychar = tm.tmHeight;
  166.  
  167.    for ( i=0; i<8; i++) {
  168.       QuerySqOrigin (0, i, &pt );
  169.       TextOut (hDC, pt.x-xchar, pt.y-BRD_VERT/2-ychar/2,
  170.          (reverse ? "87654321"+i : "12345678"+i) ,1);
  171.       
  172.       QuerySqOrigin (i,0, &pt );
  173.       TextOut (hDC, pt.x+BRD_HORZFRONT/2-xchar/2, pt.y+BRD_EDGE,
  174.          (reverse ? "hgfedcba"+i : "abcdefgh"+i), 1);
  175.  
  176.    }
  177.    
  178.    SelectObject (hDC, hOldFont);
  179.    DeleteObject ( hFont);
  180.  
  181.    SetBkColor ( hDC, OldBkColor);
  182.    SetBkMode ( hDC, OldBkMode);
  183.    SetTextColor (hDC, OldTextColor);
  184. }
  185.  
  186.  
  187. void DrawWindowBackGround ( HDC hDC, HWND hWnd, DWORD bkcolor)
  188. {
  189.    RECT rect;
  190.    HBRUSH hBrush, hOldBrush;
  191.  
  192.    hBrush = CreateSolidBrush ( bkcolor);
  193.    hOldBrush = SelectObject ( hDC, hBrush);
  194.    GetClientRect ( hWnd, &rect);
  195.    FillRect ( hDC, &rect, hBrush);
  196.    SelectObject ( hDC, hOldBrush);
  197.    DeleteObject ( hBrush);
  198. }
  199.  
  200. void HiliteSquare ( HWND hWnd, int Square )
  201. {
  202.    HDC hDC;
  203.    int x,y;
  204.    POINT aptl[4];
  205.    HRGN hRgn;
  206.  
  207.    y = Square / 8;
  208.    x = Square % 8;
  209.  
  210.    QuerySqCoords ( x,y, aptl+0);
  211.    hRgn = CreatePolygonRgn( aptl, 4, WINDING);
  212.  
  213.    hDC = GetDC ( hWnd);
  214.    InvertRgn ( hDC, hRgn );
  215.    ReleaseDC ( hWnd, hDC );
  216.  
  217.    DeleteObject ( hRgn);
  218.    HilitSq = Square;
  219. }
  220.  
  221. void UnHiliteSquare ( HWND hWnd, int Square )
  222. {
  223.    HDC hDC;
  224.    int x,y;
  225.    POINT aptl[4];
  226.    HRGN hRgn;
  227.  
  228.    if ( HilitSq == -1 ) return;
  229.  
  230.    y = Square / 8;
  231.    x = Square % 8;
  232.  
  233.    QuerySqCoords ( x,y, aptl+0);
  234.    hRgn = CreatePolygonRgn( aptl, 4, WINDING);
  235.  
  236.    hDC = GetDC ( hWnd);
  237.    InvertRgn ( hDC, hRgn );
  238.    ReleaseDC ( hWnd, hDC );
  239.  
  240.    DeleteObject ( hRgn);
  241.  
  242.    HilitSq = -1;
  243. }
  244.  
  245.